package com.yumijinfu.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yumijinfu.R;
import com.yumijinfu.base.BaseActivity;
import com.yumijinfu.entity.BaoLiInfo;
import com.yumijinfu.entity.BorrowList;
import com.yumijinfu.http.HttpCallBack;
import com.yumijinfu.http.HttpUtils;
import com.yumijinfu.mian.Config;
import com.yumijinfu.response.RenChouDetailsRes;
import com.yumijinfu.view.TitleBar;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 *   h5 加载详情页数据
 *
 */
public class DetalInforDetailsActivity extends BaseActivity {

    @Bind(R.id.titlebar)
    TitleBar titlebar;

    private BorrowList borroInfo;
    private BaoLiInfo mBaoLiInfo;
    private RenChouDetailsRes mRenChouDetailsRes;
    private String borrowType;

    //
    @Bind(R.id.pb_loading)
    ProgressBar pbLoading;
    @Bind(R.id.mWebView)
    WebView vWVContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_detal_infor_details);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        borrowType=getIntent().getStringExtra("borrowType");
        borroInfo = (BorrowList) getIntent().getSerializableExtra("borroInfo");
        mBaoLiInfo = (BaoLiInfo) getIntent().getSerializableExtra("mBaoLiInfo");
        mRenChouDetailsRes= (RenChouDetailsRes) getIntent().getSerializableExtra("mRenChouDetailsRes");
    }

    @Override
    protected void initView() {

        if (null != borroInfo) {
            titlebar.setTitleBar(R.drawable.back_selector, borroInfo.getBorrowTitle(), 0);
        }
        if (null != mBaoLiInfo) {
            titlebar.setTitleBar(R.drawable.back_selector, mBaoLiInfo.getBorrowTitle(), 0);
        }
        if(null!=mRenChouDetailsRes){
            titlebar.setTitleBar(R.drawable.back_selector, mRenChouDetailsRes.getBorrowTitle(), 0);
        }
        //初始化WebView
        vWVContent.setWebViewClient(new WebViewClient());
        vWVContent.getSettings().setJavaScriptEnabled(true);
        vWVContent.getSettings().setBuiltInZoomControls(true); //显示放大缩小 controler
        vWVContent.getSettings().setSupportZoom(true); //可以缩放
        //屏幕自适应
        vWVContent.getSettings().setUseWideViewPort(true);
        vWVContent.getSettings().setLoadWithOverviewMode(true);
        vWVContent.setWebChromeClient(new WebChromeClient(){

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
                super.onProgressChanged(view, newProgress);
                pbLoading.setProgress(newProgress);
                if(newProgress==100){
                    pbLoading.setVisibility(View.GONE);
                }

            }

        });


    }

    @Override
    protected void setData() {


        getData();
    }

    /**
     *
     * 加载h5页面
     *
     */
    private void getData() {

        HttpParams params=new HttpParams();
        if(null != borroInfo){
            params.put("borrowEId",borroInfo.getEid());
            params.put("borrowType",borrowType);
        }
        if(null != mBaoLiInfo){
            params.put("borrowEId",mBaoLiInfo.getEid());
            params.put("borrowType",borrowType);
        }
        if(null!=mRenChouDetailsRes){
            params.put("borrowEId",mRenChouDetailsRes.getBorrowEId());
            params.put("borrowType",borrowType);
        }

        HttpUtils.post(Config.DETALINFOR, params, new HttpCallBack(this,"加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {

                if(null!=result){

                    try {
                        String url=  result.getString("url");
                        vWVContent.loadUrl(url);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }



            }

            @Override
            public void onError(String code, String message) {

                Toast.makeText(DetalInforDetailsActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });





    }

    @OnClick({R.id.titlebar_iv_back})
    @Override
    protected void widgetClick(View view) {

        switch (view.getId()) {

            case R.id.titlebar_iv_back:

                finish();

            default:
        }

    }
}
